<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="user_id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="user_id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userUsername">
      <el-input v-model="dataForm.userUsername" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="订单总额" prop="totalAmount">
      <el-input v-model="dataForm.totalAmount" placeholder="订单总额"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="payType">
      <el-input v-model="dataForm.payType" placeholder="支付方式"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="收货人姓名" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收货人姓名"></el-input>
    </el-form-item>
    <el-form-item label="收货人电话" prop="receiverPhone">
      <el-input v-model="dataForm.receiverPhone" placeholder="收货人电话"></el-input>
    </el-form-item>
    <el-form-item label="收货人邮编" prop="receiverPostCode">
      <el-input v-model="dataForm.receiverPostCode" placeholder="收货人邮编"></el-input>
    </el-form-item>
    <el-form-item label="省份/直辖市" prop="receiverProvince">
      <el-input v-model="dataForm.receiverProvince" placeholder="省份/直辖市"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="receiverCity">
      <el-input v-model="dataForm.receiverCity" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="receiverRegion">
      <el-input v-model="dataForm.receiverRegion" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="receiverDetailAddress">
      <el-input v-model="dataForm.receiverDetailAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="确认收货状态" prop="confirmStatus">
      <el-input v-model="dataForm.confirmStatus" placeholder="确认收货状态"></el-input>
    </el-form-item>
    <el-form-item label="删除状态" prop="deleteStatus">
      <el-input v-model="dataForm.deleteStatus" placeholder="删除状态"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="paymentTime">
      <el-input v-model="dataForm.paymentTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="发货时间" prop="deliveryTime">
      <el-input v-model="dataForm.deliveryTime" placeholder="发货时间"></el-input>
    </el-form-item>
    <el-form-item label="确认收货时间" prop="receiveTime">
      <el-input v-model="dataForm.receiveTime" placeholder="确认收货时间"></el-input>
    </el-form-item>
    <el-form-item label="评价时间" prop="commentTime">
      <el-input v-model="dataForm.commentTime" placeholder="评价时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          userUsername: '',
          orderSn: '',
          totalAmount: '',
          payType: '',
          status: '',
          receiverName: '',
          receiverPhone: '',
          receiverPostCode: '',
          receiverProvince: '',
          receiverCity: '',
          receiverRegion: '',
          receiverDetailAddress: '',
          confirmStatus: '',
          deleteStatus: '',
          paymentTime: '',
          deliveryTime: '',
          receiveTime: '',
          commentTime: '',
          createTime: '',
          greetingMessage: '',
          note: ''
        },
        dataRule: {
          userId: [
            { required: false, message: 'user_id不能为空', trigger: 'blur' }
          ],
          userUsername: [
            { required: false, message: '用户名不能为空', trigger: 'blur' }
          ],
          orderSn: [
            { required: false, message: '订单号不能为空', trigger: 'blur' }
          ],
          totalAmount: [
            { required: false, message: '订单总额不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: false, message: '支付方式不能为空', trigger: 'blur' }
          ],
          status: [
            { required: false, message: '订单状态不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: false, message: '收货人姓名不能为空', trigger: 'blur' }
          ],
          receiverPhone: [
            { required: false, message: '收货人电话不能为空', trigger: 'blur' }
          ],
          receiverPostCode: [
            { required: false, message: '收货人邮编不能为空', trigger: 'blur' }
          ],
          receiverProvince: [
            { required: false, message: '省份/直辖市不能为空', trigger: 'blur' }
          ],
          receiverCity: [
            { required: false, message: '城市不能为空', trigger: 'blur' }
          ],
          receiverRegion: [
            { required: false, message: '区不能为空', trigger: 'blur' }
          ],
          receiverDetailAddress: [
            { required: false, message: '详细地址不能为空', trigger: 'blur' }
          ],
          confirmStatus: [
            { required: false, message: '确认收货状态不能为空', trigger: 'blur' }
          ],
          deleteStatus: [
            { required: false, message: '删除状态不能为空', trigger: 'blur' }
          ],
          paymentTime: [
            { required: false, message: '支付时间不能为空', trigger: 'blur' }
          ],
          deliveryTime: [
            { required: false, message: '发货时间不能为空', trigger: 'blur' }
          ],
          receiveTime: [
            { required: false, message: '确认收货时间不能为空', trigger: 'blur' }
          ],
          commentTime: [
            { required: false, message: '评价时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: false, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornApi(`/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.order.userId
                this.dataForm.userUsername = data.order.userUsername
                this.dataForm.orderSn = data.order.orderSn
                this.dataForm.totalAmount = data.order.totalAmount
                this.dataForm.payType = data.order.payType
                this.dataForm.status = data.order.status
                this.dataForm.receiverName = data.order.receiverName
                this.dataForm.receiverPhone = data.order.receiverPhone
                this.dataForm.receiverPostCode = data.order.receiverPostCode
                this.dataForm.receiverProvince = data.order.receiverProvince
                this.dataForm.receiverCity = data.order.receiverCity
                this.dataForm.receiverRegion = data.order.receiverRegion
                this.dataForm.receiverDetailAddress = data.order.receiverDetailAddress
                this.dataForm.confirmStatus = data.order.confirmStatus
                this.dataForm.deleteStatus = data.order.deleteStatus
                this.dataForm.paymentTime = data.order.paymentTime
                this.dataForm.deliveryTime = data.order.deliveryTime
                this.dataForm.receiveTime = data.order.receiveTime
                this.dataForm.commentTime = data.order.commentTime
                this.dataForm.createTime = data.order.createTime
                this.dataForm.greetingMessage = data.order.greetingMessage
                this.dataForm.note = data.order.note
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornApi(`/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'userUsername': this.dataForm.userUsername,
                'orderSn': this.dataForm.orderSn,
                'totalAmount': this.dataForm.totalAmount,
                'payType': this.dataForm.payType,
                'status': this.dataForm.status,
                'receiverName': this.dataForm.receiverName,
                'receiverPhone': this.dataForm.receiverPhone,
                'receiverPostCode': this.dataForm.receiverPostCode,
                'receiverProvince': this.dataForm.receiverProvince,
                'receiverCity': this.dataForm.receiverCity,
                'receiverRegion': this.dataForm.receiverRegion,
                'receiverDetailAddress': this.dataForm.receiverDetailAddress,
                'confirmStatus': this.dataForm.confirmStatus,
                'deleteStatus': this.dataForm.deleteStatus,
                'paymentTime': this.dataForm.paymentTime,
                'deliveryTime': this.dataForm.deliveryTime,
                'receiveTime': this.dataForm.receiveTime,
                'commentTime': this.dataForm.commentTime,
                'createTime': this.dataForm.createTime,
                'greetingMessage': this.dataForm.greetingMessage,
                'note': this.dataForm.note
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
